var http = require('http')
var server = http.createServer()

server.on('request', function (req, res) {

    console.log('请求我的客户端的地址是：', req.socket.remoteAddress, req.socket.remotePort)

    var url = req.url
    if (url === '/') {
        res.end('index page')
    } else if (url === '/login') {
        res.end('login page')
    } else if (url === '/products') {
        var products = [{name: '苹果 X', price: 8888},{name: '苹果 X', price: 8888}]
        // 响应内容只能是二进制数据或者字符串
        res.end(JSON.stringify(products))
    } else {
        res.end('404 Not Found.')
    }
})

server.listen(3000, function () {
    console.log('服务器启动成功，可以访问了。。。')
})
